<template>
  <div class="app-container rank-page">
    <!-- 顶部筛选栏 -->
    <el-card class="filter-card" shadow="hover">
      <el-form :inline="true" :model="filterForm">
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="filterForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :picker-options="pickerOptions"
          />
        </el-form-item>
        <el-form-item label="分类">
          <el-select v-model="filterForm.category" placeholder="全部">
            <el-option label="总榜" value="all" />
            <el-option label="月度" value="month" />
            <el-option label="周榜" value="week" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" icon="el-icon-search">查询</el-button>
          <el-button @click="resetFilter" icon="el-icon-refresh">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 当前用户排名卡片 -->
    <transition name="slide-fade">
      <el-card class="user-rank-card" shadow="hover" v-if="showUserCard">
        <div class="user-info">
          <el-avatar :size="60" :src="currentUserRank.avatar" class="avatar-pulse" />
          <div class="meta">
            <h3>您的排名: <span class="highlight">NO.{{ currentUserRank.rank }}</span></h3>
            <p>积分: {{ currentUserRank.score }} | 超越 {{ currentUserRank.exceed }}% 用户</p>
          </div>
          <div class="medal">
            <div class="crown" v-if="currentUserRank.rank <= 3">
              <i class="el-icon-star-on"></i>
            </div>
            <el-progress
              type="circle"
              :percentage="currentUserRank.progress"
              :width="70"
              :stroke-width="8"
              :color="customColors"
              class="progress-circle"
            />
          </div>
        </div>
      </el-card>
    </transition>

    <!-- 加载骨架屏 -->
    <div v-if="loading" class="skeleton-container">
      <div v-for="i in 5" :key="i" class="skeleton-item">
        <el-skeleton :rows="1" animated />
      </div>
    </div>

    <!-- 排行榜表格 -->
    <transition-group name="list" tag="div">
      <el-table
        v-if="!loading"
        :data="rankList"
        style="width: 100%"
        row-class-name="rank-row"
        @row-click="handleRowClick"
        key="table"
      >
        <el-table-column label="排名" width="80" align="center">
          <template #default="{ row }">
            <span :class="['rank-badge', getRankClass(row.rank)]">
              <transition name="bounce">
                <span :key="row.rank">{{ row.rank }}</span>
              </transition>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="用户" width="180">
          <template #default="{ row }">
            <div class="user-cell">
              <el-avatar :size="40" :src="row.avatar" class="avatar-hover" />
              <span class="name">{{ row.name }}</span>
              <i class="el-icon-medal" v-if="row.rank <= 3"></i>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="score" label="积分" sortable align="center">
          <template #default="{ row }">
            <el-tag :type="getScoreTagType(row.score)" effect="dark">
              {{ row.score }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="进度" width="200" align="center">
          <template #default="{ row }">
            <el-progress
              :percentage="row.progress"
              :stroke-width="16"
              :color="customColors"
              :show-text="false"
            />
            <span class="progress-text">{{ row.progress }}%</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120" align="center">
          <template #default="{ row }">
            <el-button
              size="mini"
              type="text"
              @click.stop="viewDetail(row)"
              class="action-btn"
            >
              详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </transition-group>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pagination.current"
        :page-sizes="[10, 20, 50]"
        :page-size="pagination.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total"
      />
    </div>

    <!-- 浮动奖杯按钮 -->
    <div class="floating-trophy" @click="scrollToTop">
      <i class="el-icon-trophy"></i>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Rank',
  data() {
    return {
      loading: true,
      showUserCard: false,
      // 筛选表单
      filterForm: {
        dateRange: [],
        category: 'all'
      },
      // 日期选择器选项
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      // 排行榜模拟数据
      rankList: [
        { rank: 1, name: '张三', avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png', score: 9850, progress: 98 },
        { rank: 2, name: '李四', avatar: 'https://cube.elemecdn.com/0/88/03b0d39583f48206768a7534e55bcpng.png', score: 8765, progress: 87 },
        { rank: 3, name: '王五', avatar: 'https://cube.elemecdn.com/9/bb/e27878a8d7e8e1e4f769ae7db1b8dpng.png', score: 7654, progress: 76 },
        { rank: 4, name: '赵六', avatar: 'https://cube.elemecdn.com/2/d9/e042a8cbf9060a1b6b4b7c8d4a20apng.png', score: 6543, progress: 65 },
        { rank: 5, name: '钱七', avatar: 'https://cube.elemecdn.com/e/f5/3fc6bd5e74d2a4e3271a3c4eb8f37png.png', score: 5432, progress: 54 },
        { rank: 6, name: '孙八', avatar: 'https://cube.elemecdn.com/8/6a/63a2d774f74a314f4b6bd7ecbf2f6png.png', score: 4321, progress: 43 },
        { rank: 7, name: '周九', avatar: 'https://cube.elemecdn.com/a/b9/e207a3d4b81d9a7d0b5e326c3284apng.png', score: 3210, progress: 32 },
        { rank: 8, name: '吴十', avatar: 'https://cube.elemecdn.com/7/3e/9a4b8b5f4d6a7e0f3b8e1d6c7e8fpng.png', score: 2109, progress: 21 },
        { rank: 9, name: '郑十一', avatar: 'https://cube.elemecdn.com/1/8e/9a3b5c7d0e1f2g3h4i5j6k7l8m9n.png', score: 1098, progress: 10 },
        { rank: 10, name: '王十二', avatar: 'https://cube.elemecdn.com/4/5e/3ea6beec64369c2642b92c6726f1epng.png', score: 987, progress: 9 }
      ],
      // 当前用户模拟数据
      currentUserRank: {
        rank: 15,
        name: '当前用户',
        avatar: 'https://n.sinaimg.cn/sinakd20116/160/w1024h1536/20230420/c176-b264957436114255e56cf6f3fbf0186a.jpg',
        score: 4567,
        progress: 45,
        exceed: 72
      },
      // 分页配置
      pagination: {
        current: 1,
        size: 10,
        total: 100
      },
      // 进度条颜色
      customColors: [
        { color: '#f56c6c', percentage: 20 },
        { color: '#e6a23c', percentage: 40 },
        { color: '#67c23a', percentage: 60 },
        { color: '#409eff', percentage: 80 }
      ]
    }
  },
  mounted() {
    // 模拟数据加载
    setTimeout(() => {
      this.loading = false;
      this.showUserCard = true;
    }, 1500);
  },
  methods: {
    // 获取排名样式类
    getRankClass(rank) {
      if (rank === 1) return 'top1'
      if (rank === 2) return 'top2'
      if (rank === 3) return 'top3'
      return ''
    },

    // 获取积分标签类型
    getScoreTagType(score) {
      if (score > 8000) return 'danger'
      if (score > 6000) return 'warning'
      if (score > 4000) return 'success'
      if (score > 2000) return ''
      return 'info'
    },

    // 查询
    handleSearch() {
      this.loading = true;
      this.showUserCard = false;

      // 模拟API调用
      setTimeout(() => {
        this.$message.success(`查询条件: ${JSON.stringify(this.filterForm)}`)
        this.pagination.current = 1
        this.loading = false;
        this.showUserCard = true;
      }, 1000);
    },

    // 重置筛选
    resetFilter() {
      this.filterForm = {
        dateRange: [],
        category: 'all'
      }
      this.handleSearch()
    },

    // 分页大小变化
    handleSizeChange(val) {
      this.pagination.size = val
      this.$message.info(`每页 ${val} 条`)
      // 实际项目中这里调用API
    },

    // 当前页变化
    handleCurrentChange(val) {
      this.pagination.current = val
      this.$message.info(`第 ${val} 页`)
      // 实际项目中这里调用API
    },

    // 查看详情
    viewDetail(row) {
      this.$notify({
        title: '用户详情',
        message: `正在查看 ${row.name} 的详情`,
        type: 'info',
        position: 'bottom-right'
      });
      // 实际项目中跳转详情页:
      // this.$router.push(`/user/detail/${row.id}`)
    },

    // 行点击事件
    handleRowClick(row) {
      this.viewDetail(row)
    },

    // 滚动到顶部
    scrollToTop() {
      window.scrollTo({
        top: 0,
        behavior: 'smooth'
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.rank-page {
  padding: 20px;
  position: relative;

  .filter-card {
    margin-bottom: 20px;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    .el-form-item {
      margin-bottom: 0;
    }
  }

  .user-rank-card {
    margin-bottom: 20px;
    background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%);
    border: none;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    transition: all 0.3s;

    &:hover {
      box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
    }

    .user-info {
      display: flex;
      align-items: center;
      padding: 10px;
      position: relative;

      .meta {
        margin-left: 15px;
        h3 {
          margin: 0 0 5px 0;
          font-size: 18px;
        }
        p {
          margin: 0;
          color: #909399;
        }
        .highlight {
          color: #f56c6c;
          font-size: 24px;
          font-weight: bold;
        }
      }

      .medal {
        position: absolute;
        right: 20px;
        display: flex;
        align-items: center;

        .crown {
          margin-right: 15px;
          font-size: 24px;
          color: #ffd700;
          animation: pulse 2s infinite;
        }

        .progress-circle {
          transition: all 0.5s;

          &:hover {
            transform: scale(1.1);
          }
        }
      }
    }
  }

  /* 表格样式 */
  .rank-row {
    cursor: pointer;
    transition: all 0.3s;

    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
  }

  /* 排名徽章 */
  .rank-badge {
    display: inline-block;
    width: 30px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    border-radius: 50%;
    font-weight: bold;
    transition: all 0.3s;

    &.top1 {
      background: linear-gradient(135deg, #ffd700 0%, #ffac00 100%);
      color: white;
      box-shadow: 0 0 0 2px rgba(255, 215, 0, 0.3);
    }
    &.top2 {
      background: linear-gradient(135deg, #c0c0c0 0%, #a0a0a0 100%);
      color: white;
      box-shadow: 0 0 0 2px rgba(192, 192, 192, 0.3);
    }
    &.top3 {
      background: linear-gradient(135deg, #cd7f32 0%, #b87333 100%);
      color: white;
      box-shadow: 0 0 0 2px rgba(205, 127, 50, 0.3);
    }
  }

  /* 用户单元格 */
  .user-cell {
    display: flex;
    align-items: center;
    position: relative;

    .name {
      margin-left: 10px;
      font-weight: 500;
    }

    .el-icon-medal {
      margin-left: 8px;
      color: #ffd700;
      font-size: 18px;
    }
  }

  /* 进度条文本 */
  .progress-text {
    display: inline-block;
    margin-top: 5px;
    font-size: 12px;
    color: #67c23a;
    transition: all 0.3s;
  }

  /* 分页容器 */
  .pagination-container {
    margin-top: 20px;
    text-align: center;
  }

  /* 骨架屏样式 */
  .skeleton-container {
    .skeleton-item {
      margin-bottom: 15px;
      padding: 15px;
      background: #fff;
      border-radius: 4px;
      box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    }
  }

  /* 浮动奖杯按钮 */
  .floating-trophy {
    position: fixed;
    bottom: 120px;
    right: 40px;
    width: 50px;
    height: 50px;
    background: linear-gradient(135deg, #ffd700 0%, #ffac00 100%);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 24px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    cursor: pointer;
    transition: all 0.3s;
    z-index: 100;

    &:hover {
      transform: scale(1.1) rotate(15deg);
      box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3);
    }
  }

  /* 头像动画 */
  .avatar-pulse {
    animation: pulse 2s infinite;
  }

  .avatar-hover {
    transition: all 0.3s;

    &:hover {
      transform: scale(1.1);
      box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    }
  }

  /* 操作按钮动画 */
  .action-btn {
    position: relative;
    overflow: hidden;

    &:after {
      content: '';
      position: absolute;
      top: 50%;
      left: 50%;
      width: 5px;
      height: 5px;
      background: rgba(255, 255, 255, 0.5);
      opacity: 0;
      border-radius: 100%;
      transform: scale(1, 1) translate(-50%);
      transform-origin: 50% 50%;
    }

    &:focus:not(:active)::after {
      animation: ripple 1s ease-out;
    }
  }
}

/* 动画定义 */
@keyframes pulse {
  0% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(255, 215, 0, 0.4);
  }
  70% {
    transform: scale(1.05);
    box-shadow: 0 0 0 10px rgba(255, 215, 0, 0);
  }
  100% {
    transform: scale(1);
    box-shadow: 0 0 0 0 rgba(255, 215, 0, 0);
  }
}

@keyframes ripple {
  0% {
    transform: scale(0, 0);
    opacity: 1;
  }
  20% {
    transform: scale(25, 25);
    opacity: 1;
  }
  100% {
    opacity: 0;
    transform: scale(40, 40);
  }
}

/* 过渡动画 */
.slide-fade-enter-active {
  transition: all 0.5s ease;
}
.slide-fade-leave-active {
  transition: all 0.3s cubic-bezier(1.0, 0.5, 0.8, 1.0);
}
.slide-fade-enter, .slide-fade-leave-to {
  transform: translateX(20px);
  opacity: 0;
}

.list-move {
  transition: transform 0.5s;
}

.bounce-enter-active {
  animation: bounce-in 0.5s;
}
.bounce-leave-active {
  animation: bounce-in 0.5s reverse;
}
@keyframes bounce-in {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1.2);
  }
  100% {
    transform: scale(1);
  }
}
</style>
